/**
 * 
 */
package com.nari.heatshare.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.IBuildingUnit;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.IBuildingUnitService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.heatshare.model.IShareAreaResult;
import com.nari.heatshare.model.IShareSinkResult;
import com.nari.heatshare.model.IUserShareResult;
import com.nari.heatshare.service.IShareAreaResultService;
import com.nari.heatshare.service.IShareSinkResultService;
import com.nari.heatshare.service.IUserShareResultService;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResultWithPageCond;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-19
 */
public class UserShareResultAction extends JSONActionSupport {
	private static final long serialVersionUID = -852935922234788170L;
	private IUserShareResultService iUserShareResultService;
	private IShareAreaResultService iShareAreaResultService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatRoomService iHeatRoomService;
	private IBuildingUnitService iBuildingUnitService;
	private IShareSinkResultService iShareSinkResultService;
	//用户分摊信息查询
	private String projectUID;
	private String communityUID;
	private String buildingUID;
	private String heatRoomUID;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	private List<IHeatRoom> heatRoomList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IUserShareResult> userShareResultPage;
	private Map<String, String> pageShareAreaResultMap;
	private Map<String, String> pageProjectMap;
	private Map<String, String> pageCommunityMap;
	private Map<String, String> pageBuildingMap;
	private Map<String, String> pageHeatRoomMap;
	private Map<String, String> pageShareResultMap;
	//用户分摊详细资料
	private String userShareResultUID;
	private IUserShareResult userShareResult;
	private IShareAreaResult shareAreaResult;
	private String districtCityID;
	private String capitalCityID;
	private String provinceCityID;
	private Map<String, String> cityMap;
	private IProject project;
	private ICommunity community;
	private IBuilding building;
	private IBuildingUnit buildingUnit;
	private IHeatRoom heatRoom;
	private List<IShareSinkResult> shareSinkResultList;
	//分摊域分摊所属用户分摊信息查询
	private String areaResultUID;
	private String roomOwner;
	private String houseNo;
	
	/**
	 * 分页查询用户分摊结果列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-5
	 */
	public String queryUserResultPage() throws Exception {
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//所属热用户
		this.heatRoomList = this.iHeatRoomService.queryHeatRoomsByBelongs(projectUID, communityUID, null, buildingUID);
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.userShareResultPage = iUserShareResultService.queryUserShareResultPage(projectUID, communityUID, buildingUID, heatRoomUID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.userShareResultPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userShareResultPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//项目映射表
		this.pageProjectMap = new HashMap<String, String>();
		for (IProject project : this.projectList) {
			this.pageProjectMap.put(project.getId(), project.getProjectName());
		}
		//小区映射表
		this.pageCommunityMap = iUserShareResultService.queryReferCommunityMap(userShareResultPage.getList());
		//楼栋映射表
		this.pageBuildingMap = iUserShareResultService.queryReferBuildingMap(userShareResultPage.getList());
		//热用户映射表
		this.pageHeatRoomMap = iUserShareResultService.queryReferRoomMap(userShareResultPage.getList());
		//分摊域分摊映射表
		this.pageShareAreaResultMap = iUserShareResultService.queryReferShareResultMap(userShareResultPage.getList());
		return "userResultPage";
	}
	
	/**
	 * 查询用户分摊详细资料
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-6
	 */
	public String queryUserResultDetail() throws Exception {
		if(this.userShareResultUID!=null && !"".equals(userShareResultUID)) {
			this.userShareResult = iUserShareResultService.queryEntityById(userShareResultUID);
			//所属分摊域分摊
			this.shareAreaResult = iShareAreaResultService.queryEntityById(userShareResult.getAreaResultID());
			//所属项目
			this.project = iProjectService.queryEntityById(userShareResult.getProjectID());
			//所属小区
			this.community = iCommunityService.queryEntityById(userShareResult.getCommunityID());
			//所属城市
			this.cityMap = iUserShareResultService.queryReferCityMap(Arrays.asList(new IUserShareResult[]{userShareResult}));
			this.provinceCityID = this.community.getProvinceCityID();
			this.capitalCityID = this.community.getCapitalCityID();
			this.districtCityID = this.community.getDistrictCityID();
			//所属楼栋
			this.building = iBuildingService.queryEntityById(userShareResult.getBuildingID());
			//所属楼栋单元
			this.buildingUnit = iBuildingUnitService.queryEntityById(userShareResult.getUnitID());
			//所属热用户
			this.heatRoom = iHeatRoomService.queryEntityById(userShareResult.getRoomId());
			//所属散热器分摊列表
			this.shareSinkResultList = iShareSinkResultService.queryByUserResultUID(userShareResultUID);
		}
		return "userResultDetail";
	}
	
	/**
	 * 通过分摊域分摊ID分页查询所属的用户分摊列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-10-16
	 */
	public String queryUserPageByAreaResultUID() throws Exception {
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.userShareResultPage = iUserShareResultService.queryUserPageByAreaResultUID(areaResultUID, roomOwner, houseNo, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.userShareResultPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.userShareResultPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//楼栋映射表
		this.pageBuildingMap = iUserShareResultService.queryReferBuildingMap(userShareResultPage.getList());
		return "areaUserBillPage";
	}
	
	/**
	 * @return the iUserShareResultService
	 */
	@JSON(serialize=false)
	public IUserShareResultService getiUserShareResultService() {
		return iUserShareResultService;
	}
	
	/**
	 * @param iUserShareResultService the iUserShareResultService to set
	 */
	public void setiUserShareResultService(
			IUserShareResultService iUserShareResultService) {
		this.iUserShareResultService = iUserShareResultService;
	}

	/**
	 * @return the iShareAreaResultService
	 */
	@JSON(serialize=false)
	public IShareAreaResultService getiShareAreaResultService() {
		return iShareAreaResultService;
	}

	/**
	 * @param iShareAreaResultService the iShareAreaResultService to set
	 */
	public void setiShareAreaResultService(
			IShareAreaResultService iShareAreaResultService) {
		this.iShareAreaResultService = iShareAreaResultService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iHeatRoomService
	 */
	@JSON(serialize=false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	/**
	 * @param iHeatRoomService the iHeatRoomService to set
	 */
	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	/**
	 * @return the iBuildingUnitService
	 */
	@JSON(serialize=false)
	public IBuildingUnitService getiBuildingUnitService() {
		return iBuildingUnitService;
	}

	/**
	 * @param iBuildingUnitService the iBuildingUnitService to set
	 */
	public void setiBuildingUnitService(IBuildingUnitService iBuildingUnitService) {
		this.iBuildingUnitService = iBuildingUnitService;
	}

	/**
	 * @return the iShareSinkResultService
	 */
	@JSON(serialize=false)
	public IShareSinkResultService getiShareSinkResultService() {
		return iShareSinkResultService;
	}

	/**
	 * @param iShareSinkResultService the iShareSinkResultService to set
	 */
	public void setiShareSinkResultService(
			IShareSinkResultService iShareSinkResultService) {
		this.iShareSinkResultService = iShareSinkResultService;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the heatRoomUID
	 */
	public String getHeatRoomUID() {
		return heatRoomUID;
	}

	/**
	 * @param heatRoomUID the heatRoomUID to set
	 */
	public void setHeatRoomUID(String heatRoomUID) {
		this.heatRoomUID = heatRoomUID;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the heatRoomList
	 */
	public List<IHeatRoom> getHeatRoomList() {
		return heatRoomList;
	}

	/**
	 * @param heatRoomList the heatRoomList to set
	 */
	public void setHeatRoomList(List<IHeatRoom> heatRoomList) {
		this.heatRoomList = heatRoomList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the userShareResultPage
	 */
	public ResultWithPageCond<IUserShareResult> getUserShareResultPage() {
		return userShareResultPage;
	}

	/**
	 * @param userShareResultPage the userShareResultPage to set
	 */
	public void setUserShareResultPage(
			ResultWithPageCond<IUserShareResult> userShareResultPage) {
		this.userShareResultPage = userShareResultPage;
	}

	/**
	 * @return the pageShareAreaResultMap
	 */
	public Map<String, String> getPageShareAreaResultMap() {
		return pageShareAreaResultMap;
	}

	/**
	 * @param pageShareAreaResultMap the pageShareAreaResultMap to set
	 */
	public void setPageShareAreaResultMap(Map<String, String> pageShareAreaResultMap) {
		this.pageShareAreaResultMap = pageShareAreaResultMap;
	}

	/**
	 * @return the pageProjectMap
	 */
	public Map<String, String> getPageProjectMap() {
		return pageProjectMap;
	}

	/**
	 * @param pageProjectMap the pageProjectMap to set
	 */
	public void setPageProjectMap(Map<String, String> pageProjectMap) {
		this.pageProjectMap = pageProjectMap;
	}

	/**
	 * @return the pageCommunityMap
	 */
	public Map<String, String> getPageCommunityMap() {
		return pageCommunityMap;
	}

	/**
	 * @param pageCommunityMap the pageCommunityMap to set
	 */
	public void setPageCommunityMap(Map<String, String> pageCommunityMap) {
		this.pageCommunityMap = pageCommunityMap;
	}

	/**
	 * @return the pageBuildingMap
	 */
	public Map<String, String> getPageBuildingMap() {
		return pageBuildingMap;
	}

	/**
	 * @param pageBuildingMap the pageBuildingMap to set
	 */
	public void setPageBuildingMap(Map<String, String> pageBuildingMap) {
		this.pageBuildingMap = pageBuildingMap;
	}

	/**
	 * @return the pageHeatRoomMap
	 */
	public Map<String, String> getPageHeatRoomMap() {
		return pageHeatRoomMap;
	}

	/**
	 * @param pageHeatRoomMap the pageHeatRoomMap to set
	 */
	public void setPageHeatRoomMap(Map<String, String> pageHeatRoomMap) {
		this.pageHeatRoomMap = pageHeatRoomMap;
	}

	/**
	 * @return the pageShareResultMap
	 */
	public Map<String, String> getPageShareResultMap() {
		return pageShareResultMap;
	}

	/**
	 * @param pageShareResultMap the pageShareResultMap to set
	 */
	public void setPageShareResultMap(Map<String, String> pageShareResultMap) {
		this.pageShareResultMap = pageShareResultMap;
	}

	/**
	 * @return the userShareResultUID
	 */
	public String getUserShareResultUID() {
		return userShareResultUID;
	}

	/**
	 * @param userShareResultUID the userShareResultUID to set
	 */
	public void setUserShareResultUID(String userShareResultUID) {
		this.userShareResultUID = userShareResultUID;
	}

	/**
	 * @return the userShareResult
	 */
	public IUserShareResult getUserShareResult() {
		return userShareResult;
	}

	/**
	 * @param userShareResult the userShareResult to set
	 */
	public void setUserShareResult(IUserShareResult userShareResult) {
		this.userShareResult = userShareResult;
	}

	/**
	 * @return the shareAreaResult
	 */
	public IShareAreaResult getShareAreaResult() {
		return shareAreaResult;
	}

	/**
	 * @param shareAreaResult the shareAreaResult to set
	 */
	public void setShareAreaResult(IShareAreaResult shareAreaResult) {
		this.shareAreaResult = shareAreaResult;
	}

	/**
	 * @return the districtCityID
	 */
	public String getDistrictCityID() {
		return districtCityID;
	}

	/**
	 * @param districtCityID the districtCityID to set
	 */
	public void setDistrictCityID(String districtCityID) {
		this.districtCityID = districtCityID;
	}

	/**
	 * @return the capitalCityID
	 */
	public String getCapitalCityID() {
		return capitalCityID;
	}

	/**
	 * @param capitalCityID the capitalCityID to set
	 */
	public void setCapitalCityID(String capitalCityID) {
		this.capitalCityID = capitalCityID;
	}

	/**
	 * @return the provinceCityID
	 */
	public String getProvinceCityID() {
		return provinceCityID;
	}

	/**
	 * @param provinceCityID the provinceCityID to set
	 */
	public void setProvinceCityID(String provinceCityID) {
		this.provinceCityID = provinceCityID;
	}

	/**
	 * @return the cityMap
	 */
	public Map<String, String> getCityMap() {
		return cityMap;
	}

	/**
	 * @param cityMap the cityMap to set
	 */
	public void setCityMap(Map<String, String> cityMap) {
		this.cityMap = cityMap;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the building
	 */
	public IBuilding getBuilding() {
		return building;
	}

	/**
	 * @param building the building to set
	 */
	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	/**
	 * @return the buildingUnit
	 */
	public IBuildingUnit getBuildingUnit() {
		return buildingUnit;
	}

	/**
	 * @param buildingUnit the buildingUnit to set
	 */
	public void setBuildingUnit(IBuildingUnit buildingUnit) {
		this.buildingUnit = buildingUnit;
	}

	/**
	 * @return the heatRoom
	 */
	public IHeatRoom getHeatRoom() {
		return heatRoom;
	}

	/**
	 * @param heatRoom the heatRoom to set
	 */
	public void setHeatRoom(IHeatRoom heatRoom) {
		this.heatRoom = heatRoom;
	}

	/**
	 * @return the shareSinkResultList
	 */
	public List<IShareSinkResult> getShareSinkResultList() {
		return shareSinkResultList;
	}

	/**
	 * @param shareSinkResultList the shareSinkResultList to set
	 */
	public void setShareSinkResultList(List<IShareSinkResult> shareSinkResultList) {
		this.shareSinkResultList = shareSinkResultList;
	}

	/**
	 * @return the areaResultUID
	 */
	public String getAreaResultUID() {
		return areaResultUID;
	}

	/**
	 * @param areaResultUID the areaResultUID to set
	 */
	public void setAreaResultUID(String areaResultUID) {
		this.areaResultUID = areaResultUID;
	}

	/**
	 * @return the roomOwner
	 */
	public String getRoomOwner() {
		return roomOwner;
	}

	/**
	 * @param roomOwner the roomOwner to set
	 */
	public void setRoomOwner(String roomOwner) {
		this.roomOwner = roomOwner;
	}

	/**
	 * @return the houseNo
	 */
	public String getHouseNo() {
		return houseNo;
	}

	/**
	 * @param houseNo the houseNo to set
	 */
	public void setHouseNo(String houseNo) {
		this.houseNo = houseNo;
	}
	
}
